
/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */

package loginbean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

public class DisplayProductsService 
{
	//Display a list of the products available for this shopping cart.
	//Make a form for the shopper to select an item and have it added to the cart.
	public String showAvailableProducts(int tenantid)
	{
		String strQuery, result = null;
		strQuery = "select value from datatable,objecttable,fieldtable where fieldtable.fieldid=datatable.fieldid and "
				+"fieldtable.objectid=objecttable.objectid and datatable.tenantid="+tenantid+" and objecttable.objectname='Product'"
				+" and fieldtable.fieldname='Name'";
		Vector<String> productNames = queryDataBase(strQuery);
		strQuery = "select value from datatable,objecttable,fieldtable where fieldtable.fieldid=datatable.fieldid and "
				+"fieldtable.objectid=objecttable.objectid and datatable.tenantid="+tenantid+" and objecttable.objectname='Product'"
				+" and fieldtable.fieldname='Price'";
		Vector<String> productPrices = queryDataBase(strQuery);
		
		result = "<TABLE BORDER=\"1\"><TR><TH>Item Name</TH><TH>Item Price</TH></TR>";
		for(int i=0; i < productNames.size(); i++)
		{
			result += "<TR><TD>"+productNames.get(i)+"</TD>";
			result += "<TD>"+productPrices.get(i)+"</TD>";
			result += "<TD><FORM METHOD=\"POST\" ACTION=\"Result2.jsp\">";
			result += "<INPUT TYPE=\"HIDDEN\" NAME=\"add\" VALUE=\""+ productNames.get(i) +"\" />";
			result += "<INPUT TYPE=\"SUBMIT\" VALUE=\"Add To Cart\" />";
			result += "</FORM></TD>";
			result += "<TD><FORM METHOD=\"POST\" ACTION=\"Result2.jsp\">";
			result += "<INPUT TYPE=\"HIDDEN\" NAME=\"detail\" VALUE=\""+ productNames.get(i) +"\" />";
			result += "<INPUT TYPE=\"SUBMIT\" VALUE=\"View Product Details\" />";
			result += "</FORM></TD></TR>";			
		}
		
		result += "</TABLE>";

		return result;
	}
	private static Vector<String> queryDataBase(String query)
	{
		Vector<String> results = new Vector<String>();
		try
		{
			DBManager db = new DBManager();
			Connection conn = db.getDatabaseConnection();
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(query);
			while(rs.next())
			{
				results.addElement(rs.getString(1));
			}
			conn.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return results;
	}
	private static void insertIntoDataBase(String query)
	{
		try
		{
			DBManager db = new DBManager();
			Connection conn = db.getDatabaseConnection();
			Statement st = conn.createStatement();
			st.execute(query);
			conn.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
